package com.agritech.observer;

import java.util.ArrayList;
import java.util.List;

public class EnvironmentSensor implements Subject {
    private List<Observer> observers = new ArrayList<>();
    private String environmentData;

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
        System.out.println(observer.getClass().getSimpleName() + " 已注册到环境传感器.");
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
        System.out.println(observer.getClass().getSimpleName() + " 已从环境传感器移除.");
    }

    @Override
    public void notifyObservers(String message) {
        System.out.println("环境传感器发出通知: " + message);
        for (Observer observer : observers) {
            observer.update(message);
        }
    }

    public void setData(String data) {
        this.environmentData = data;
        System.out.println("环境传感器采集到新数据: " + data);
        notifyObservers(data); // 数据变化时通知观察者
    }

    public String getEnvironmentData() {
        return environmentData;
    }
}